#! /bin/sh

CWD=`cd $(dirname $0);pwd;`
HOSTFILE="$CWD/hosts.txt"
CMD=`basename $0`

if [ $# -ge 3 ]; then
  id="$1"
  file="$2"
  dest="$3"
elif [ $# -ge 2 ];then
  id="$1"
  file="$2"
  dest="/tmp/"
else
  echo "$CMD server_id file [dest_path]"
  exit
fi

line=`grep "^$id:" $HOSTFILE`

if [[ $line =~ "^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)$" ]]; then
  user=${BASH_REMATCH[2]}
  pass=${BASH_REMATCH[3]}
  host=${BASH_REMATCH[4]}
  port=${BASH_REMATCH[5]}
fi

exec ${CWD}/${CMD}.exp $user $pass $host $port $file $dest
